<template>
  <el-container class="homeContainer">
    <!-- 头部 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="" />
        <span>后台管理系统</span>
      </div>
      <el-button type="info" @click="loginOut">退出</el-button>
    </el-header>
    <!-- 主体 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle" @click="toggleMenu">|||</div>
        <!-- 菜单 -->
        <el-menu
          background-color="rgb(107, 123, 175)"
          text-color="rgb(243, 246, 248)"
          active-text-color="rgb(209, 138, 203)"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="list.id + ''"
            v-for="list in menulist"
            :key="list.id"
          >
            <!-- 一级菜单模板区 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsObj[list.id]"></i>
              <!-- 文本 -->
              <span>{{ list.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + child.path"
              v-for="child in list.children"
              :key="child.id"
              @click="saveNavState('/' + child.path)"
            >
              <!-- 二级菜单模板区 -->
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{ child.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 内容 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'Home',
  data() {
    return {
      // 左侧菜单数据
      menulist: [],
      // 用id来赋值图标
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao',
      },
      // 设置是否折叠的默认值
      isCollapse: false,
      // 被激活的链接地址
      activePath: '',
    }
  },
  created() {
    this.getMenuList(),
      (this.activePath = window.sessionStorage.getItem('activePath'))
  },
  methods: {
    loginOut() {
      // 清空token
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取所有菜单
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta, msg)
      this.menulist = res.data
    },
    // 点击折叠展开侧边栏
    toggleMenu() {
      this.isCollapse = !this.isCollapse
    },
    // 保存链接的激活状态
    saveNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
    },
  },
}
</script>

<style scoped>
.homeContainer {
  height: 100%;
}
.el-header {
  background-color: rgb(107, 123, 175);
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: rgb(243, 246, 248);
  font-size: 20px;
}
.el-header div {
  display: flex;
  align-items: center;
}
.el-header div span {
  margin-left: 20px;
}
.el-aside {
  background-color: rgb(92, 106, 155);
}
.el-aside .el-menu {
  border-right: none;
}
.el-main {
  background-color: rgb(250, 247, 248);
}
.iconfont {
  margin-right: 20px;
}
.toggle {
  background-color: rgb(131, 131, 211);
  font-size: 10px;
  line-height: 30px;
  color: rgb(245, 247, 248);
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
